//
//  LightningViewController.m
//  LINNER
//
//  Created by wei wang on 2017/3/30.
//  Copyright © 2017年 weiwang. All rights reserved.
//

#import "LightningViewController.h"
#import "LightningSetViewController.h"
#import "LightningModel.h"

@interface LightningViewController ()

@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *modeBtn;

@property (weak, nonatomic) IBOutlet UIImageView *modeImage;

/** 文字数组 **/
@property(nonatomic,strong) NSArray *titleArray;
/** imageArray **/
@property(nonatomic,strong) NSArray *imageArray;
@end

@implementation LightningViewController
- (void)viewWillAppear:(BOOL)animated{
//    [AppDelegate globalDelegate].deviceToConnect = [DeviceToConnect sharedController];

    [super viewWillAppear:animated];
    [self setNavigationStyle];
    [self getLightningStatus];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [[AppDelegate globalDelegate].deviceToConnect findEQlocation];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.imageArray = @[@"bg_equilibrium",@"bg_bass",@"bg_voice",@"bg_treble"];
    self.titleArray = @[@"均衡模式",@"低音增强",@"人声增强",@"高音增强"];
    
}

- (void)setNavigationStyle{
    
    [self setNavigationBarTitle:MyNSLocalizedString(@"NC21 Lightning")];
    [self setLeftButtonImage:[UIImage imageNamed:@"tittle_back_nor"]];
    [self.navigationController.navigationBar makeTransparent_nornav];
    [self setRightButtonImage:[UIImage imageNamed:@"icon_set"]];
    
}

- (void)getLightningStatus{
    NSLog(@"当前选中：%li",(long)[AppDelegate globalDelegate].deviceToConnect.eqstate);
    
//    if ([[AppDelegate globalDelegate].deviceToConnect findEQlocation])
//    {
//
//        UIButton *selectBtn = self.modeBtn[[AppDelegate globalDelegate].deviceToConnect.eqstate];
//        [self changeMode:selectBtn];
//    }
    NSArray *array = [LightningModel selectAllObject];
    LightningModel *model = array.lastObject;
    NSInteger eqstate = [model.eqstate intValue];
    UIButton *selectBtn = self.modeBtn[eqstate];
    [self changeMode:selectBtn];
}


- (IBAction)changeMode:(UIButton *)sender {
    
    for (UIButton *btn in _modeBtn) {
        
        btn.selected = NO;
    }
    
    UIButton *Btn = (UIButton *)[self.view viewWithTag:sender.tag];
    
    self.modeImage.image = [UIImage imageNamed:self.imageArray[sender.tag-1]];
    Btn.selected = YES;
    
    NSInteger number = sender.tag - 1;
    
    [Btn setTitle:MyNSLocalizedString(self.titleArray[number]) forState:UIControlStateNormal];
    [[AppDelegate globalDelegate].deviceToConnect eqController:number];
    NSLog(@"当前控制：%li",(long)number);
/*  查询耳机发送指令是否正确
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self getLightningStatus];
    });
 */
    NSArray *array = [LightningModel selectAllObject];
    LightningModel *model = array.lastObject;
    model.eqstate = [NSString stringWithFormat:@"%li",(long)number];
    [model insertObject];
}

- (void)onRightButtonClick:(id)sender{
    
    LightningSetViewController *vc = [LightningSetViewController new];
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
